package com.onenine.app.view.popwin;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.onenine.app.R;

/**
 * 创建时间：2018/5/18 0018 15:28
 * 创建人：fly
 * 功能描述：通用底部弹层
 */
public abstract class BottomPopWindow extends PopupWindow implements PopupWindow.OnDismissListener {

    protected Activity mActivity;

    public BottomPopWindow(Activity activity) {
        super(activity);
        mActivity = activity;
        initPopWindow();
        setContentView(genContentView());
    }

    private void initPopWindow() {
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        setAnimationStyle(R.style.BottomInOutPopWin);
        backgroundAlpha(0.5f);
        setOnDismissListener(this);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    protected void backgroundAlpha(float bgAlpha) {
        Window window = mActivity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = bgAlpha;
        window.setAttributes(lp);
    }

    /**
     * 从底部弹出弹层
     */
    public void showPopWindow() {
        showAtLocation(mActivity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1.0f);
    }

    protected abstract View genContentView();

}
